﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    public partial class OrderNetwork : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        //private Dictionary<string, object> dicQueryParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        OrderNetworkData m_OrderNetworkData;
        public DataRow dwABPOS;
        public DataRow dwVKPOS;
        public DataSet dsParamaters;       
        #endregion
        public OrderNetwork()
        {
            InitializeComponent();
            this.Name = "订单网络";
            
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent(); 
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";

        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                //dsParamaters = ((IntexReportClient.DataSetIntex)(this.FindResource("dataSetIntex")));
                InitDataBinding();
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT VKPKUNDNR,KDKUNDNA,VKPPOSTYP,VKPAUFKRS,VKPAUFLFD,VKPAUFPOS,VKPARTST, "
                    + " VKPKOLL,VKPART,VKPFARBE,VKPKDARTNR,VKPOFFEN/100 AS OPENQTY,VKPRESERV/100 AS PLANQTY, "
                    + " VKPZUGET/100 AS ALLOCATEQTY, VKPFREIGE/100 AS CONFIRMQTY ,VKPVERS/100 AS DELIVERYQTY, "
                    + " VKPFAKT/100 AS INVQTY,VKPMEEH,VKPNEUDAT,VKPWUTERM,VKPLIEFEING,VKPKDKOMM,VKPZUSATZ "
                    + " FROM VKPOS,KDSTAMM WHERE VKPMANDANT = KDMANDANT AND VKPKUNDNR = KDKUNDNR "
                    + " AND VKPSA <> 'AJ' AND VKPERL <> 'J' ";


                dsResult = m_OrderNetworkData.Connect2Proxy(orlReadCommand);

                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "VKPOS";

                    byte[] b;
                    string value;
                    foreach (DataRow rowTemp in dsResult.Tables["VKPOS"].Rows)
                    {

                        foreach (DataColumn dc in dsResult.Tables["VKPOS"].Columns)
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(rowTemp[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            rowTemp[dc.ColumnName] = value;

                        }
                    }
                    foreach (DataTable dtTemp in dsResult.Tables)
                    {
                        setUI = m_OrderNetworkData.ImportDataTable(setUI, dtTemp);
                        
                    }
                }
                setUI.AcceptChanges();
                this.DataContext = setUI;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return;

        }
        private void MainDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.AddedItems.Count == 0)
                    return;

                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwVKPOS = drv.Row;
                this.txtVKPAUFKRS.Text = dwVKPOS["VKPAUFKRS"].ToString();
                this.txtVKPAUFLFD.Text = dwVKPOS["VKPAUFLFD"].ToString();
                this.txtVKPAUFPOS.Text = dwVKPOS["VKPAUFPOS"].ToString();
                
                ReloadABAUFM(dwVKPOS);
                //ReloadBabst(dwABPOS);
                //ReloadGaauf(dwABPOS);
                //ReloadABFOLGE(dwABPOS);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }

        }
        private void ReloadABAUFM(DataRow dwVKPOS)
        {

            setUI = m_OrderNetworkData.GetABAUFM(setUI, dwVKPOS);
            this.listOrderNetwork.DataContext = setUI;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            /*DataRow dwTmp = dwABPOS.Table.Clone().Rows.Add();
            dwTmp.ItemArray = dwABPOS.ItemArray;
            DataSet setRpt = new DataSet();
            //setRpt = m_OrderNetworkData.BuildProducitonOrderData(setRpt, dwABPOS);
            dwABPOS.ItemArray = dwTmp.ItemArray;
            dicParamater["ABPAUFKRS"] = dwABPOS["ABPAUFKRS"].ToString();
            dicParamater["ABPAUFLFD"] = dwABPOS["ABPAUFLFD"].ToString();
            dicParamater["ABPAUFPOS"] = dwABPOS["ABPAUFPOS"].ToString();
            WarpOrderReport m_rpt = new WarpOrderReport(dicParamater);
            m_rpt.dsSource = setRpt;
            m_rpt.GetReport("EXCEL");
             * */
        }


        private void InitDataBinding()
        {

            m_OrderNetworkData = new OrderNetworkData();
            m_OrderNetworkData.OracleDB = this.OracleDB;
            m_OrderNetworkData.proxy = this.proxy;
            this.setUI.Tables.Clear();
            
            setUI.AcceptChanges();
        }

     

    }
}
